package com.example.servicetest;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

/**
 * Created by 宇成童鞋 on 2016/10/23.
 */

public class MyService extends Service {

    private DownloadBinder mBinder = new DownloadBinder();

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    //服务启动时调用
    public void onCreate() {
        super.onCreate();
        NotificationManager manager = (NotificationManager) getSystemService
                (NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(this);
        builder.setContentTitle("This is title");//设置通知标题
        builder.setContentText("This is content");//设置通知内容
        builder.setSmallIcon(R.drawable.ic_launcher); //通知图标
        Notification notification = builder.build();//用build方法生成Notification对象
//        manager.notify(1, notification);
        startForeground(1, notification);
        Log.d("MyService", "onCreate executed");
    }

    @Override
    //服务启动时调用
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("MyService", "onStartCommand executed");
        new Thread(new Runnable() {
            @Override
            public void run() {
                //处理具体逻辑
                stopSelf();
            }
        }).start();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    //服务销毁时调用
    public void onDestroy() {
        Log.d("MyService", "onDestroy executed");
        super.onDestroy();
    }

    class DownloadBinder extends Binder {
        public void startDownload() {
            Log.d("MyService", "startDownload executed");
        }

        public int getProgress() {
            Log.d("MyService", "getProgress executed");
            return 0;
        }
    }

}
